package cn.tedu.shoot;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class ShootPanel extends JPanel implements MouseMotionListener {
    Image bgImg = new ImageIcon("plane/background.jpg").getImage();
    int bgX=0,bgY=0;
    List<Bullet> bullets = new ArrayList<Bullet>();
    List<Enemy> enemies = new ArrayList<Enemy>();
    Hero hero = new Hero();
    public ShootPanel(){
        //对事件进行监听注册
        this.addMouseMotionListener(this);
    }
    public void paint(Graphics g){
        super.paint(g);
        //绘制背景图
        paintBgImg(g);
        //画出英雄机
        paintHero(g);
        //绘制子弹
        paintBullet(g);
        //画出敌军
        paintEnemy(g);
        //绘制分数等
        paintScore(g);
    }
    public void action() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                //移动背景图片
                bgImgMove();
                //创建子弹
                bulletAction();
                //移动子弹
                bulletMove();
                //子弹越界的处理
                bulletOutOfBounds();
                //创建敌机
                enemyAction();
                //移动(动态)敌机
                enemyMoveAndStep();
                //敌机越界的处理
                enemyOutOfBounds();
                //子弹打中敌机
                bulletHitEnemy();
                //重绘
                repaint();
            }
        },0,10);

    }
    private void paintBgImg(Graphics g) {
        g.drawImage(bgImg,bgX,bgY,null);
        g.drawImage(bgImg,bgX,bgY-768,null);
    }

    private void paintHero(Graphics g) {
        g.drawImage(hero.image, hero.x, hero.y, null);
        hero.step();
    }
    private void paintEnemy(Graphics g) {
        for(int i=0;i<enemies.size();i++){
            Enemy enemy = enemies.get(i);
            g.drawImage(enemy.enemyImg,enemy.x,enemy.y,null);
        }
    }
    //绘制子弹
    private void paintBullet(Graphics g) {
        for (int i = 0;i < bullets.size();i++){
            Bullet bullet = bullets.get(i);
            g.drawImage(bullet.bulletImg,bullet.x,bullet.y,null);
        }
    }
    //绘制分数
    public void paintScore(Graphics g){
        g.setColor(Color.RED);//红色
        //Font的三个参数分别表示字体，加粗，大小
        Font f = new Font("宋体",Font.BOLD,28);
        g.setFont(f);
        g.drawString("得分: "+hero.score,350,710);
        g.drawString("金币: "+hero.gold,230,710 );
        g.drawString("生命: "+hero.life,80,710);
        g.setColor(Color.GREEN);
        g.drawString("关卡： "+(hero.score/1000+1),370,30);
    }

    //移动背景图片
    private void bgImgMove() {
        bgY++;
        //如果第一个图完全移动出，第二个完全进入窗口
        if (bgY>=768){
            bgY=0;
        }
    }
    //创建子弹
    int bulletIndex = 0;
    private void bulletAction() {
        if(bulletIndex==Integer.MAX_VALUE){
            bulletIndex=0;
        }
        //大白话：小的容器hero.shoot()的元素，倒入大的容器bullets中
        if(bulletIndex++%10==0){
            bullets.addAll(hero.shoot());
        }

    }
    //创建敌机
    int enemyIndex = 0;
    public void enemyAction(){
        if(enemyIndex==Integer.MAX_VALUE){
            enemyIndex = 0;
        }
        if(enemyIndex++%20==0){
            enemies.add(new Enemy());
        }
    }
    //移动子弹
    private void bulletMove() {
        for (int i = 0; i < bullets.size(); i++){
            bullets.get(i).move();
        }
    }
    //移动敌机
    public void enemyMoveAndStep(){
        for(int i=0;i<enemies.size();i++){
            Enemy enemy = enemies.get(i);
            enemy.moveAndStep();
        }
    }
    //子弹越界的处理
    private void bulletOutOfBounds() {
        for (int i = 0;i < bullets.size();i++){
            Bullet bullet = bullets.get(i);
            if(bullet.y<0){
                bullets.remove(bullet);
            }
        }
    }

    //敌机越界的处理
    public void enemyOutOfBounds(){
        for(int i=0;i<enemies.size();i++){
            Enemy enemy = enemies.get(i);
            if(enemy.y>ShootFrame.HEIGHT){
                enemies.remove(enemy);
            }
        }
    }

    //子弹打中敌机
    public void bulletHitEnemy(){
        //遍历得到所有的子弹，每一个子弹都和所有的敌机做比较
        for(int i=0;i<bullets.size();i++){
            Bullet bullet = bullets.get(i);
            for(int j=0;j<enemies.size();j++){
                Enemy enemy = enemies.get(j);
                if(bullet.hitEnemy(enemy)){
                    hero.score++;
                    enemies.remove(enemy);
                    bullets.remove(bullet);
                    break;//结束最近的一层循环(内层循环)，开始外层循环的下一次。
                }
            }
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        hero.x = e.getX()-hero.image.getWidth(null)/2;
        hero.y = e.getY()-hero.image.getHeight(null)/2;
    }
}
